package enterpriseCustomer

import (
	"context"
	"github.com/google/uuid"
	"github.com/kuangshp/go-utils/k"
	"github.com/spf13/cast"
	"insighttracking/apps/admin/auth"
	"insighttracking/common/utils"
	"insighttracking/query/dao"
	"insighttracking/query/model"
	"insighttracking/query/service"
	"net/http"

	"insighttracking/apps/admin/internal/svc"
	"insighttracking/apps/admin/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
	"os"
)

type GetEnterprisCustomereListApiLogic struct {
	logx.Logger
	ctx     context.Context
	svcCtx  *svc.ServiceContext
	Request *http.Request
}

func NewGetEnterprisCustomereListApiLogic(ctx context.Context, svcCtx *svc.ServiceContext, request *http.Request) *GetEnterprisCustomereListApiLogic {
	hostname, _ := os.Hostname()
	return &GetEnterprisCustomereListApiLogic{
		Logger: logx.WithContext(ctx).WithCallerSkip(0).WithFields(
			logx.Field("Log_UUID", uuid.New().String()),
			logx.Field("hostname", hostname)),
		ctx:     ctx,
		svcCtx:  svcCtx,
		Request: request,
	}
}

func (l *GetEnterprisCustomereListApiLogic) GetEnterprisCustomereListApi(req *types.GetEnterprisCustomereListReq) (resp []types.EnterpriseCustomerModel, err error) {
	accountInfo, err := auth.GetAccountInfo(l.svcCtx, l.Request)
	if err != nil {
		return make([]types.EnterpriseCustomerModel, 0), nil
	}
	tx := dao.EnterpriseCustomerEntity.WithContext(l.ctx).Where(
		dao.EnterpriseCustomerEntity.EnterpriseID.Eq(accountInfo.EnterpriseId),
		dao.EnterpriseCustomerEntity.Status.Eq(0),
	)
	if req.IsAccount != "" {
		isAccount := cast.ToInt64(req.IsAccount)
		accountEntities, err := service.AccountService.GetByAllAccountApi(l.ctx, accountInfo.EnterpriseId)
		if err != nil || len(accountEntities) == 0 {
			return make([]types.EnterpriseCustomerModel, 0), nil
		}
		customerId := k.Map(accountEntities, func(item *model.AccountEntity, index int) int64 {
			return item.CustomerID
		})
		lastCustomerId := k.Distinct(k.Filter(customerId, func(item int64, index int) bool {
			return item > 0
		}))
		logx.WithContext(l.ctx).Infof("最后的数据:%v", lastCustomerId)
		if isAccount == 1 {
			tx = tx.Where(dao.EnterpriseCustomerEntity.ID.NotIn(lastCustomerId...))
		} else if isAccount == 2 {
			tx = tx.Where(dao.EnterpriseCustomerEntity.ID.In(lastCustomerId...))
		} else {
			return make([]types.EnterpriseCustomerModel, 0), nil
		}
	}
	customerEntities, err := tx.Order(dao.EnterpriseCustomerEntity.ID.Desc()).Find()
	if err != nil || len(customerEntities) == 0 {
		return make([]types.EnterpriseCustomerModel, 0), nil
	}
	return k.Map(customerEntities, func(item *model.EnterpriseCustomerEntity, index int) types.EnterpriseCustomerModel {
		return types.EnterpriseCustomerModel{
			ID:           item.ID,                                 // 主键id
			EnterpriseID: item.EnterpriseID,                       // 关联到enterprise表主键id
			Name:         item.Name,                               // 门店名称
			Person:       item.Person,                             // 负责人
			Mobile:       item.Mobile,                             // 联系电话
			Logo:         item.Logo,                               // 状态logo
			Status:       item.Status,                             // 状态0：营业  1：停业
			Types:        item.Types,                              // 客户类型0：公司，1：门店，2：个人
			ProvinceCode: item.ProvinceCode,                       // 省份code
			CityCode:     item.CityCode,                           // 城市code
			CountyCode:   item.CountyCode,                         // 县、区code
			TownCode:     item.TownCode,                           // 乡镇code
			Address:      item.Address,                            // 具体地址
			FullAddress:  item.FullAddress,                        // 拼接省市县地址
			CreatedAt:    utils.TimestampToString(item.CreatedAt), // 创建时间
			UpdatedAt:    utils.TimestampToString(item.UpdatedAt), // 更新时间
		}
	}), nil
}
